from django.shortcuts import render
from django.views import View
from django import http
import logging
from django.core.cache import cache

from areas.models import Area
from meiduo_mall.utils.response_code import RETCODE
# Create your views here.

logger = logging.getLogger('django')

class AreasView(View):
    """省市区三级联动"""
    def get(self,request):
        # 判断当前是查询省份数据,还是市区数据
        area_id = request.GET.get('area_id')
        if not area_id:

            # 获取并判断是否有缓存
            province_list = cache.get('province_list')
            if not province_list:
                # 查省级数据
                try:
                    # Area.objects.filter(属性名__条件表达式=值)
                    # province_model_list 查询到的数据是模型列表Area
                    province_model_list = Area.objects.filter(parent__isnull=True)

                    # 需要将模型列表转成字典列表
                    province_list = []
                    for province_model in province_model_list:
                        province_dict = {
                            'id':province_model.id,
                            'name':province_model.name,
                            }
                        province_list.append(province_dict)

                    # 缓存省份字典列表数据: 默认存储到rendis别名为'default'的配置中
                    cache.set('province_list', province_list, 3600)

                    # # 响应省级JSON数据-----(if语句外执行)-----------根据经验可省略------------------------------------------------
                    # return  http.JsonResponse({'code':RETCODE.OK, 'errmsg':'OK', 'province_list':province_list})
                    #-----------------------------------------------------------------------------------------------

                except Exception as e:
                    logger.error(e)
                    return http.JsonResponse({'code':RETCODE.DBERR,'errmsg':'查询省份数据错误'})

            # 响应省级JSON数据
            return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'province_list': province_list})

        else:

            # 判断是否有城市或区县缓存
            sub_data = cache.get('sub_area' + area_id)
            if not sub_data:
                try:
                    # 查城市或区县数据(一查多)
                    parent_model = Area.objects.get(id=area_id)
                    # subs => area_set   因为related_name='subs'
                    # 在字表中定义外键时，增加related_name属性,指定这个字表在主表中对应的外键属性
                    sub_model_list = parent_model.subs.all()


                    # 将子级模型列表转成字典列表
                    subs = []
                    for sub_model in sub_model_list:
                        sub_dict = {
                            'id':sub_model.id,
                            'name':sub_model.name,
                        }
                        subs.append(sub_dict)



                    # 构造子级JSON数据
                    sub_data = {
                        'id':parent_model.id,
                        'name':parent_model.name,
                        'subs':subs,
                        }


                    # 缓存城市或区县
                    cache.set('sub_area' + area_id, sub_data, 3600)


                    # # 响应城市或者区县JSON数据结果-----(if语句外执行)-----------根据经验可省略----------
                    # return http.JsonResponse({'code':RETCODE.OK,'errmsg':'OK','sub_data':sub_data})
                    #---------------------------------------------------------------------------------
                except Exception as e:
                    logger.error(e)
                    return http.JsonResponse({'code':RETCODE.DBERR,'errmsg':'城市或区县数据错误'})

            # 响应城市或者区县JSON数据结果
            return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'sub_data': sub_data})